package cn.bugstack.xfg.frame.domain.openapi.service;

import cn.bugstack.xfg.frame.domain.openapi.model.aggregates.ChatProcessAggregate;
import cn.bugstack.xfg.frame.domain.openapi.model.entity.RuleLogicEntity;
import cn.bugstack.xfg.frame.domain.openapi.model.entity.UserAccountQuotaEntity;
import cn.bugstack.xfg.frame.domain.openapi.model.valobj.LogicCheckTypeVO;
import cn.bugstack.xfg.frame.domain.openapi.service.rule.ILogicFilter;
import cn.bugstack.xfg.frame.domain.openapi.service.rule.factory.DefaultLogicFactory;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter;

import javax.annotation.Resource;
import java.util.Map;

public class ChatService extends AbstractChatService {

    @Resource
    private DefaultLogicFactory logicFactory;

    @Override
    protected RuleLogicEntity<ChatProcessAggregate> doCheckLogic(ChatProcessAggregate chatProcess, UserAccountQuotaEntity userAccount, String... logics) throws Exception {
        Map<String, ILogicFilter<UserAccountQuotaEntity>> logicFilterMap = logicFactory.openLogicFilter();
        RuleLogicEntity<ChatProcessAggregate> entity = null;
        for (String code : logics) {
            if (DefaultLogicFactory.LogicModel.NULL.getCode().equals(code)) continue;
            // 关键：策略模式过滤
            entity = logicFilterMap.get(code).filter(chatProcess, userAccount);
            if (!LogicCheckTypeVO.SUCCESS.equals(entity.getType())) return entity;
        }
        return entity != null ? entity : RuleLogicEntity.<ChatProcessAggregate>builder()
                .type(LogicCheckTypeVO.SUCCESS).data(chatProcess).build();
    }

    @Override
    protected void doMessageResponse(ChatProcessAggregate chatProcess, ResponseBodyEmitter emitter) {

    }


}
